﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Web.Mvc;

namespace MvcSimplyCleverPart3
{
    public class MvcResourceProviderFactory : ResourceProviderFactory
    {
        public override IResourceProvider CreateGlobalResourceProvider(
            string classKey)
        {
            return GetFactory().CreateGlobalResourceProvider(classKey);
        }

        public override IResourceProvider CreateLocalResourceProvider(
            string virtualPath)
        {
            return GetFactory().CreateLocalResourceProvider(virtualPath);
        }

        private static ResourceProviderFactory GetFactory()
        {
            var resolver = DependencyResolver.Current;

            var factory = resolver.GetService<ResourceProviderFactory>();

            if (factory != null)
            {
                return factory;
            }

            throw new InvalidOperationException(string.Format(
                "No {0} has been registered in the {1}.",
                typeof(ResourceProviderFactory).FullName,
                DependencyResolver.Current.GetType().FullName));
        }
    }
}